//
//  WallectService.m
//  Bees
//
//  Created by NowYouCanSee on 2018/12/26.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "WallectService.h"
#import "EOSAccountResult.h"
@implementation WallectService
- (void)getKeyAccountsComplete:(CompleteBlock)complete{
    if (!self.publicKey.length) {
        return;
    }
    [PPNetworkHelper POST:URL_eos_get_key_accounts parameters:@{@"public_key":self.publicKey} responseCache:nil success:^(id responseObject) {
        EOSAccountResult *result = [EOSAccountResult modelWithDictionary:responseObject];
        !complete?:complete(result.message,result.account_names,YES);
    } failure:^(NSError *error) {
        !complete?:complete(kNetworkError,@[],NO);
    }];
}

- (void)getTheInvitationCodeComplete:(void(^)(void))complete{
    NSDictionary*mobileDic = @{@"mobile":@"17665201977"};
    NSDictionary*parDic = [NetworkManager creatSignWithParameter:mobileDic];
    NSString *url = [NetworkManager creatGETUrl:URL_EOS_account_Pay parameter:parDic];
    NSURL *openUrl = String_To_URL(url);
    if ([kApplication canOpenURL:openUrl]) {
        [kApplication openURL:openUrl];
    }
}
- (void)validAccountComplete:(void(^)(BOOL,NSString*errorMsg))complete{
    [NetworkManager GET:URL_is_valid_account parameters:@{@"account":self.accountName?:@""} responseCache:nil success:^(id responseObject) {
        XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
        if ([response.code isEqualToNumber:@200]) {
            !complete?:complete(YES,response.message);
        }else{
            !complete?:complete(NO,response.message);
        }
    } failure:^(NSError *error) {
        
    }];
}
- (void)creatAccoutComplete:(void(^)(BOOL,NSString*errorMsg))complete{
    if (!self.accountName.length || !self.ownerPrivateKey.eosPublicKey.length || !self.activePrivateKey.eosPublicKey.length || !self.invite_code.length) {
        return;
    }
    [NetworkManager POST:URL_EOS_creat_account parameters:@{@"account":self.accountName,@"owner_public_key":self.ownerPrivateKey.eosPublicKey,@"active_public_key":self.activePrivateKey.eosPublicKey,@"invite_code":self.invite_code} responseCache:nil success:^(id responseObject) {
        XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
        if ([response.code isEqualToNumber:@200]) {
            !complete?:complete(YES,response.message);
        }else{
            !complete?:complete(NO,response.message);
        }
    } failure:^(NSError *error) {
        
    }];
}
- (void)getTokenListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    params[@"key"]=VALIDATE_STRING(self.tokenKey);
    self.latestParams = params;
    [NetworkManager GET:URL_token parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.tokenList removeAllObjects];
            }
            [self.tokenList addObjectsFromArray:[NSArray modelArrayWithClass:[TokenInfo class] json:pageM.data]];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (NSMutableArray<TokenInfo*>*)tokenList{
    if (!_tokenList) {
        _tokenList = [NSMutableArray array];
    }
    return _tokenList;
}
@end
